package com.xie.o2o.service.impl;

import com.xie.o2o.dao.ProductCategoryDao;
import com.xie.o2o.dao.ProductDao;
import com.xie.o2o.dto.ProductCategoryExecution;
import com.xie.o2o.entity.ProductCategory;
import com.xie.o2o.enums.ProductCategoryStateEnum;
import com.xie.o2o.service.ProductCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @create: xie
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryDao productCategoryDao;
    @Autowired
    private ProductDao productDao;

    /**
     * 添加商品分类
     *
     * @param productCategoryList
     * @return
     * @throws RuntimeException
     */
    @Override
    @Transactional
    public ProductCategoryExecution batchAddProductCategoryList(List<ProductCategory> productCategoryList) throws RuntimeException {
        productCategoryDao.addProductCategoryList(productCategoryList);
        return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);

    }

    /**
     * 查询商品分类列表
     *
     * @param shopid
     * @return
     */
    @Override
    public List<ProductCategory> queryProductCategoryList(long shopid) {
        return productCategoryDao.queryProductCategoryList(shopid);
    }

    /**
     * 删除商品分类，先清除商品与categoryid的关联，然后删除类别
     *
     * @param productCategoryId
     * @param shopId
     * @return
     * @throws RuntimeException
     */
    @Override
    @Transactional
    public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId) throws RuntimeException {
        productDao.updateProductCategoryIdToNUll(productCategoryId);
        productCategoryDao.deleteProductCategory(productCategoryId, shopId);
        return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
    }
}
